<?php
// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

if (file_exists(dirname(__FILE__) . '/defines.php')) {
	include_once dirname(__FILE__) . '/defines.php';
}

if (!defined('_JDEFINES')) {
	define('JPATH_BASE', dirname(__FILE__));
	require_once JPATH_BASE.'/includes/defines.php';
}
require_once ('./configuration.php');
require_once (JPATH_BASE . '/administrator/components/com_manager/helpers/manager.php');

$config = array();
$list_product = getProduct($config);
updateProductSearch($list_product, $config);

function getProduct(&$config) {	
	$JConfig = new JConfig;
	$conn = mysql_connect($JConfig->host, $JConfig->user, $JConfig->password) or die("false: connect db failed in get");
	
	mysql_select_db($JConfig->db, $conn) or die("false: select db failed");
	mysql_query("set names 'utf8'", $conn);
	
	$query = "SELECT DISTINCT p.id as product_id, p.feed_date, p.sale_date, p.expired_date, p.status, "; 
	$query.= "se.code as serial_code, se.encrypted_code as encrypted_code, ";
	$query.= "po.name as pond_name, ";
	$query.= "cu.name as customer_name, ";
	$query.= "fc.name as fishclass_name, fc.content as fishclass_content, fc.image as fishclass_image ";  
	$query.= "FROM tbl_product p ";
	$query.= "JOIN tbl_serial as se ON(se.code = p.serial_code ) ";
	$query.= "JOIN tbl_pond as po ON(po.code = p.pond_code ) ";
	$query.= "JOIN tbl_fish_class as fc ON(fc.code = p.class_code ) ";
	$query.= "JOIN tbl_customer as cu ON(cu.code = p.customer_code ) ";
	$query.= "WHERE p.status != " . ManagerHelper::PRODUCT_STATUS_EXPIRED . " "; 
	$query.= "ORDER BY p.id";
	
	$q = mysql_query($query) or die("false: service error");
	while($r = mysql_fetch_array($q)) {
		$items[] = $r;
	}
	
	// Get all config
	$query_config = " SELECT * FROM tbl_configpim" ;
	$result = mysql_query($query_config) or die("false: service error");
	while($r = mysql_fetch_array($result)) {
		$key = $r['pim_key'];
		$config[$key] = $r['pim_value'];
	}
	
	return ($items);
}

function updateProductSearch($listProduct, $config) {
	$conn = mysql_connect($config['remote_ip'], $config['remote_username'], $config['remote_password']) or die("false: connect db failed in update");
	
	mysql_select_db($config['remote_database'], $conn) or die("false: select db failed");
	mysql_query("set names 'utf8'", $conn);
	
	//$sql_delete = 'DELETE FROM tbl_product';
	//mysql_query($sql_delete);
	
	//Get list products
	$sql_get_used_product = "SELECT * FROM tbl_product ";
	$q = mysql_query($sql_get_used_product) or die("false: get used products failed");
	$arr_product_id = array();
	while($r = mysql_fetch_array($q)) {
		$arr_product_id[] = $r['product_id'];
	}
	
	$sql_insert = 'INSERT INTO tbl_product(product_id, serial_code, encrypted_code, pond_name, fishclass_name, fishclass_content, fishclass_image, customer_name, feed_date, sale_date, expired_date, status) ';
	$str_value = "";
	
	foreach ($listProduct as $product) {
		if (!in_array($product['product_id'], $arr_product_id)) {
			$product['fishclass_image'] = $_SERVER['HTTP_HOST'].ManagerHelper::FOLDER_UPLOAD.$product['fishclass_image'];
			
			if ($str_value == "") {
				$str_value.=' VALUES("'.$product['product_id'].'", "'.$product['serial_code'].'", "'.$product['encrypted_code'].'", "'.$product['pond_name'].'", "'.$product['fishclass_name'].'","'.$product['fishclass_content'].'","'.$product['fishclass_image'].'","'.$product['customer_name'].'","'.$product['feed_date'].'","'.$product['sale_date'].'","'.$product['expired_date'].'", "'.ManagerHelper::getTextStatus($product['status']).'")';	
			} else {
				$str_value.=',("'.$product['product_id'].'", "'.$product['serial_code'].'","'.$product['encrypted_code'].'", "'.$product['pond_name'].'", "'.$product['fishclass_name'].'","'.$product['fishclass_content'].'","'.$product['fishclass_image'].'","'.$product['customer_name'].'","'.$product['feed_date'].'","'.$product['sale_date'].'","'.$product['expired_date'].'", "'.ManagerHelper::getTextStatus($product['status']).'")';
			}
		}		
	}
	
	$q = mysql_query($sql_insert.$str_value);
	echo $q;
}

exit;
?>